﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using System.Text;
using Autofac.Core;

namespace Cloak.Mvp.Autofac.ViewParameters
{
	/// <summary>
	/// Provides the view during resolution of a presenter instance
	/// </summary>
	public class ViewParameter : ConstantParameter
	{
		/// <summary>
		/// Initializes a parameter with the specified view and parameter specification
		/// </summary>
		/// <param name="view">The view to be passed to the presenter</param>
		/// <param name="specification">The specification which determines view parameters in presenters' constructors</param>
		public ViewParameter(object view, IViewParameterSpecification specification)
			: base(view, GetIsViewPredicate(specification))
		{}

		private static Predicate<ParameterInfo> GetIsViewPredicate(IViewParameterSpecification specification)
		{
			Contract.Requires(specification != null);

			return parameter => specification.IsView(parameter);
		}
	}
}